package com.mftour.moudles.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mftour.common.Constant;
import com.mftour.moudles.entity.User;
import com.mftour.moudles.service.TestService;
import com.mftour.moudles.vo.AjaxResponse;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/test")
public class TestController extends BaseController {

	@Autowired
	TestService testSer;

	@ApiOperation(value = "测试spring事物回滚", notes = "不做任何捕获处理，页面报错")
	@RequestMapping(value = "/ajax/save", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse save() {
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加user失败！");
		request.setAttribute("ajaxResponse", ajaxResponse);
		testSer.save1();
		return ajaxResponse;
	}
	
	@ApiOperation(value = "测试有效期", notes = "")
	@RequestMapping(value = "/ajax/save0", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse save0() {
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加user0失败！");
//		request.setAttribute("ajaxResponse", ajaxResponse);
		testSer.save1();
		return ajaxResponse;
	}

	@ApiOperation(value = "测试spring事物回滚", notes = "controller中捕获处理")
	@RequestMapping(value = "/ajax/save1", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse save1() {
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "添加test成功！");
		try {
			testSer.save1();
		} catch (Exception e) {
			e.printStackTrace();
			ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加test失败！");
		}
		return ajaxResponse;
	}

	@ApiOperation(value = "测试spring事物回滚", notes = "service中手动回滚，controller无法捕获异常")
	@RequestMapping(value = "/ajax/save2", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse save2() {
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "添加test成功！");
		if (!testSer.save2()) {
			ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加test失败！");
		}
		return ajaxResponse;
	}

	@ApiOperation(value = "测试spring400错误")
	@RequestMapping(value = "/ajax/test400", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse test400(@RequestBody User u) {
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加user失败！");
		session.setAttribute("ajaxResponse", ajaxResponse);
		return ajaxResponse;
	}
	
	@ResponseBody
	@RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
	public AjaxResponse downloadWarnArea() throws IOException {
		AjaxResponse ajaxResponse = new AjaxResponse();
		String filePath = getFilePath();
		File file = new File(filePath);
		String fileName = new String("区域级告警信息统计报表.xlsx".getBytes("UTF-8"), "iso-8859-1");
		// 设置response
		response.setContentType("application/x-msdownload; charset=UTF-8");
		response.addHeader("content-type", "application/x-msdownload;");
		response.addHeader("content-disposition", "attachment; filename=" + fileName);
		response.setContentLength((int) file.length());
		// 输出
		InputStream in = new FileInputStream(file);
		OutputStream os = response.getOutputStream();
		try {
			int length = 0;
			byte[] buffer = new byte[1024];
			while ((length = in.read(buffer)) != -1) {
				os.write(buffer, 0, length);
			}
			os.flush();
		} finally {
			if (in != null) {
				in.close();
			}
		}
		return ajaxResponse;
	}

	public String getFilePath() {
		testSer.exportToExcel(null);
		return testSer.exportToExcel();
	}
}
